import java.util.Scanner;

public class Eater_Allience {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        // 初始化3个数组存储菜品、价格、点赞数
        String[] dishNames={"红烧带鱼","鱼香肉丝","时令鲜蔬"};//菜品名
        double[] prices=new double[]{38.0,20.0,10.0};
        int[] praiseNums=new int[3];
        // 在初始化6个数组用于存储订单信息
        String[] names=new String[4];//订单人姓名
        String[] dishMegs=new String[4];//保存所选信息，包括菜品名及份数
        int[] dishNums=new int[4];//保存选了几份
        int[] times=new int[4];//保存送餐时间
        String[] addresses=new String[4];//保存送餐地址
        double[] sumPrices=new double[4];//保存订单的总金额
        int[] states=new int[4];//保存订单状态：0已预定  1已完成
        //初始化2条订单信息
        names[0]="张晴";
        dishMegs[0]="红烧带鱼";
        dishNums[0]=2;
        times[0]=12;
        addresses[0]="天成路207号";
        sumPrices[0]=76.0;
        states[0]=1;
        names[1]="张晴";
        dishMegs[1]="鱼香肉丝";
        dishNums[1]=3;
        times[1]=18;
        addresses[1]="天成路207号";
        sumPrices[1]=45.0;
        states[1]=0;
        /* 实现菜单切换 */
        System.out.println("欢迎使用“吃货联盟订餐系统”");
        int num=-1;//用户输入0返回系统，否则推出系统
        boolean isExit=false;//标志用户是否退出系统：true为推出系统,false 未退出系统
        do{
            System.out.println("********************************************");
            System.out.println("1.我要订餐");
            System.out.println("2.查看餐袋");
            System.out.println("3.签收订单");
            System.out.println("4.删除订单");
            System.out.println("5.我要点赞");
            System.out.println("6.退出系统");
            System.out.println("********************************************");
            System.out.println("请选择：");
            int choose=in.nextInt();
            /* 使用switch结构来建立选择内容 */
            switch (choose){
                case 1://我要订餐
                    System.out.println("***我要订餐***");
                    boolean isAdd=false;//记录是否可以点餐
                    for (int i = 0; i <names.length ; i++) {
                        if (names[i]==null){
                            isAdd=false;
                            System.out.println("请输入订餐人姓名：");
                            String name=in.next();
                            System.out.println("序号"+"\t菜名"+"\t\t单价"+"\t点赞数");
                            for (int j = 0; j <dishNames.length ; j++) {
                                System.out.println((j+1)+"\t"+dishNames[j]+"\t\t"+prices[j]+"元"+"\t"+praiseNums[j]);
                            }
                            System.out.print("请输入您要点的菜品编号：");
                            int chooseDish=in.nextInt();
                            String dishMeg=dishNames[chooseDish-1];
                            System.out.print("请选择您要的份数：");
                            int number=in.nextInt();
                            System.out.print("请输入送餐时间（送餐时间在10-20点之间整点送餐）：");
                            int time=in.nextInt();
                            while (time<10||time>20){
                                System.out.print("您输入的时间有误，请输入10-20之间的整数。");
                                time=in.nextInt();
                            }
                            System.out.print("请输入送餐地址：");
                            String address=in.next();
                            // 默认可以预定，不用写订单状态
                            System.out.println("订餐成功");
                            System.out.println("您定的是"+dishNames[chooseDish-1]+number+"份");
                            System.out.println("送餐时间"+time+"点");
                            double sumPrice=prices[chooseDish-1]*number;//计算餐费
                            double sendMoney=(sumPrice>50)?0:5;//计算送餐费
                            System.out.println("餐费："+sumPrice+"元，送餐费："+sendMoney+"元，总计："+(sumPrice+sendMoney)+"元");
                            //添加数据
                            names[i]=name;
                            dishMegs[i]=dishMeg;
                            times[i]=time;
                            dishNums[i]=number;
                            addresses[i]=address;
                            sumPrices[i]=sumPrice+sendMoney;
                            break;
                        }
                        if (isAdd){
                            System.out.println("对不起，您的餐袋已满！");
                        }
                    }
                    break;
                case 2://查看餐袋
                    System.out.println("***查看餐袋***");
                    System.out.println("序号\t订餐人\t餐品名称\t餐品数量\t送餐时间\t送餐地址\t总金额\t订单状态");
                    for (int i = 0; i <names.length ; i++) {
                        if (names[i]!=null){
                            String state = (states[i] == 0) ? "已预订" : "已完成";
                            System.out.println((i + 1) + "\t\t" + names[i] + "\t" + dishMegs[i] + "\t\t" + dishNums[i] + "份"
                                    + "\t\t\t" + times[i] + "点\t" + addresses[i] + "\t" + sumPrices[i] + "元" + "\t" + state);
                        }
                    }
                    break;
                case 3://签收订单
                    System.out.println("***签收订单***");
                    boolean isSignFind=false;
                    System.out.print("请选择要签收的订单号：");
                    int sign=in.nextInt();
                    for (int i = 0; i < names.length; i++) {
                        if (names[i]!=null&&states[i]==0&&sign==i+1 ){
                            states[i]=1;
                            System.out.println("订单签收成功");
                            isSignFind=true;
                        }
                        else if(names[i]!=null&&states[i]==1&&sign==i+1){
                            System.out.println("您选择的订单已签收，不可再次签收");
                            isSignFind=true;
                        }
                    }
                    if (!isSignFind){
                        System.out.println("您输入的订单不存在！");
                    }
                    break;
                case 4://删除订单
                    System.out.println("***删除订单***");
                    boolean isDelFind=false;
                    System.out.println("请选择要删除的订单号：");
                    int delId=in.nextInt();
                    for (int i = 0; i <names.length ; i++) {
                        //根据状态值判断能不能删除，只有已完成的才能删除
                        if (names[i]!=null&&states[i]==1&&delId==i+1){
                            isDelFind=true;
                            for (int j = delId-1; j <names.length-1 ; j++) {
                                names[j]=names[j+1];
                                dishMegs[j]=dishMegs[j+1];
                                dishNums[j]=dishNums[j+1];
                                states[j]=states[j+1];
                                addresses[i]=addresses[i+1];
                                times[j]=times[j+1];
                                sumPrices[i]=sumPrices[j+1];
                            }
                            //将最后一位清空
                            names[names.length-1]=null;
                            dishMegs[names.length-1]=null;
                            dishNums[names.length-1]=0;
                            states[names.length-1]=0;
                            addresses[names.length-1]=null;
                            times[names.length-1]=0;
                            sumPrices[names.length-1]=0;
                            System.out.println("订单删除成功！");
                            break;
                        }else if (names[i]!=null&&states[i]==0&&delId==i+1){
                            isDelFind=true;
                            System.out.println("您选择的订单未签收，不能删除！");
                            break;
                        }
                    }if (!isDelFind){
                    System.out.println("您选择的订单不存在，不能删除！");
                }
                    break;
                case 5://我要点赞
                    System.out.println("***我要点赞***");
                    //选择要点赞的菜品
                    System.out.println("序号\t菜品\t单价");
                    for (int i = 0; i < dishNames.length; i++) {
                        String priaiseNum=(praiseNums[i]>0)?praiseNums[i]+"赞":" ";
                        System.out.println((i+1)+"\t"+dishNames[i]+"\t\t"+prices[i]+"元");
                    }
                    System.out.print("请选择您要点赞的菜品序号：");
                    int number=in.nextInt();
                    praiseNums[number-1]++;
                    System.out.println("点赞成功");
                    break;
                case 6://退出系统
                    isExit=true;
                    break;
                default://退出系统
                    isExit=true;
                    break;
            }
            //返回主界面
            if(!isExit){
                System.out.println("输入0返回");
                num=in.nextInt();
                } else {
                    break;
                }
        } while (num == 0);
        System.out.println("谢谢惠顾，欢迎您再次使用！");
        in.close();
    }
}

